package tv.youxing.android.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.entity.LiShiPlayerModel;
import tv.youxing.android.entity.VideoPlayerPostModel;
import tv.youxing.android.ui.SubscribeDeteilsActivity;

/**
 * Created by Administrator on 2017/6/25.
 * 历史播放记录
 */

public class LiShiPlayerAdapter extends BaseAdapter {

    private Context context;
    private List<LiShiPlayerModel.ResultBean.DataBean> mDataBeanList = new ArrayList<>();
    private LayoutInflater layoutInflater;


    public LiShiPlayerAdapter(Context context, List<LiShiPlayerModel.ResultBean.DataBean> dataBeanList) {
        this.context = context;
        mDataBeanList = dataBeanList;
        layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mDataBeanList==null || mDataBeanList.size()==0 ? 0 : mDataBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (viewHolder==null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.lishiplayer_item, null);

            viewHolder.rootView = convertView.findViewById(R.id.rootView);
            viewHolder.head = convertView.findViewById(R.id.head);
            viewHolder.title = convertView.findViewById(R.id.title);
            viewHolder.player_count = convertView.findViewById(R.id.play_count);
            viewHolder.common_count = convertView.findViewById(R.id.comment_count);
            viewHolder.time = convertView.findViewById(R.id.time);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }





        ImageLoader.getInstance().displayImage(mDataBeanList.get(position).getCover(),viewHolder.head);
        viewHolder.title.setText(mDataBeanList.get(position).getTitle());
        viewHolder.player_count.setText(mDataBeanList.get(position).getPlay_count()+" 播放");
        viewHolder.common_count.setText(mDataBeanList.get(position).getComment_count()+" 评论");
        viewHolder.time.setText(mDataBeanList.get(position).getPublish_at()+"");



        viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LiShiPlayerModel.ResultBean.DataBean m = mDataBeanList.get(position);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count());
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setAlbum_name(m.getAlbum_name());
                model.setIs_collectied(m.getIs_collectied());
                model.setIs_like(m.getIs_like());
                model.setVideo_share_url(m.getVideo_share_url());
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(context, SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",model);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        return convertView;
    }


    class ViewHolder{
        private LinearLayout rootView;
        private ImageView head;
        private TextView title;
        private TextView player_count;
        private TextView common_count;
        private TextView time;
    }
}
